package main

import (
	"fmt"
	"github.com/pterm/pterm"
	"io/ioutil"
	"os"
	"xmediaEmu/pkg/decoder"
	"xmediaEmu/pkg/decoder/codec"
)

const PCMAPayLoadLength = 160

type G711WavPayloader struct{}

func (p *G711WavPayloader) Payload(wav_data []byte) (rtp_payload [][]byte) {
	// 确定语音数据的起始位置.
	start := 0
	for i := 36; i < len(wav_data); i++ { //RIFF和fmt块最小为36位
		if string(wav_data[i:i+4]) == "data" {
			start = i + 8
			break
		}
	}
	if start == 0 {
		return rtp_payload
	}
	pterm.FgYellow.Printf("wav_data length is:%d start is:%d\n", len(wav_data), start)

	// 语音帧...
	for index := start; index < len(wav_data); index += PCMAPayLoadLength {
		// 跳过不足.
		if index+PCMAPayLoadLength > len(wav_data) {
			return
		}
		rtp_payload = append(rtp_payload, wav_data[index:index+PCMAPayLoadLength])
		// pterm.FgYellow.Printf("rtp_payload length is:[%d], \n", len(rtp_payload))
	}
	return rtp_payload
}

// for test..
func main() {
	if len(os.Args) < 2 {
		fmt.Println("Please provide a audio file")
		os.Exit(1)
	}

	codeData := codec.NewPCMAlawCodecData()
	g711Decoder, err := decoder.NewAudioDecoder(codeData)
	if err != nil {
		panic(err)
	}
	// 初始化编码.
	if err = g711Decoder.Setup(); err != nil {
		panic(err)
	}

	// read file for test..
	// f,err := os.OpenFile(, os.O_RDWR, os.FileMode(0666) )
	// if err != nil {
	// 	fmt.Printf("Unable to open file %s, error: %s\n", os.Args[1], err.Error())
	// 	os.Exit(1)
	// }
	// g711文件
	fileBuf, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		fmt.Printf("Unable to open file %s, error: %s\n", os.Args[1], err.Error())
		os.Exit(1)
	}

	// 711 pay load to every buf.
	var g711Loader G711WavPayloader
	payload711AWav := g711Loader.Payload(fileBuf)

	// 一帧帧解析.
	for _, pkt := range payload711AWav {
		// 1024成一帧.
		got, frameResult, err := g711Decoder.Decode(pkt)
		if len(frameResult.Data) > 0 {
			fmt.Printf("Decode result length is: got:%v frameResult.Data[0]:length:%d frameResult.Data:length:%d err:%v\n", got, len(frameResult.Data[0]), len(frameResult.Data), err)
		} else {
			fmt.Printf("Decode result 0: got:%v err:%v\n", got, err)
		}
	}
	//var bytesBuff []byte
	//for _, pkt := range payload711AWav {
	//	bytesBuff = append(bytesBuff, pkt...)
	//	if len(bytesBuff) < AudioInBufSize {
	//		continue
	//	}
	//	// 1024成一帧.
	//	got, frameResult, err := g711Decoder.Decode(bytesBuff)
	//	if len(frameResult.Data) > 0 {
	//		fmt.Printf("Decode result length is: got:%v frameResult.Data[0]:length:%d frameResult.Data:length:%d err:%v\n", got, len(frameResult.Data[0]), len(frameResult.Data), err)
	//	} else {
	//		fmt.Printf("Decode result 0: got:%v err:%v\n", got, err)
	//	}
	//
	//	// 截断长度继续.
	//	bytesBuff = bytesBuff[AudioInBufSize:]
	//}
	//
	//if len(bytesBuff) > 0 {
	//	got, frameResult, err := g711Decoder.Decode(bytesBuff)
	//	if len(frameResult.Data) > 0 {
	//		fmt.Printf("Decode result length is: got:%v frameResult.Data[0]:length:%d frameResult.Data:length:%d err:%v\n", got, len(frameResult.Data[0]), len(frameResult.Data), err)
	//	} else {
	//		fmt.Printf("Decode result 0: got:%v err:%v\n", got, err)
	//	}
	//}

	g711Decoder.Close()
}
